Managing memory in a computer system

ABSTRACT

Methods, computer program products, and systems for managing memory in a computer system in which memory locations in use at any given time are represented as a set of memory objects in a first object graph. The first object graph includes a system root object associated by references to each of the memory objects. A method includes creating a second root object for the memory so as to form a second object graph for the memory. The method also includes, in response to the dereferencing of a first object from the first object graph, associating the dereferenced first object with the second object graph so that the second object graph includes at least one dereferenced object.

PRIORITY

This application claims priority to Great Britain Patent Application No. 1208434.9, filed 15 May 2012, and all the benefits accruing therefrom under 35 U.S.C. §119, the contents of which in its entirety are herein incorporated by reference.

BACKGROUND

The present invention relates to a computer system, and more particularly, to managing memory in a computer system.

Computer systems commonly use a virtual memory management system. Virtual memory management systems use dynamic memory allocation processes and garbage collection processes to respectively allocate and reclaim memory allocations. The garbage collection process is arranged to identify allocated but unusable memory allocations, clear the associated memory objects they store and return the identified memory allocations for reuse by reallocation.

The garbage collection process requires significant processing power and may delay other processing by the computer. The memory efficiency of application programs can be improved so that fewer discarded memory objects are produced for the garbage collection process to clear. However, with multiple system or application programs running on a given computer, identifying the source of discarded memory objects is difficult and time consuming.

SUMMARY

Embodiments include methods, computer program products, and systems for managing memory in a computer system in which memory locations in use at any given time are represented as a set of memory objects in a first object graph. The first object graph includes a system root object associated by references to each of the memory objects. A method includes creating a second root object for the memory so as to form a second object graph for the memory. The method also includes, in response to the dereferencing of a first object from the first object graph, associating the dereferenced first object with the second object graph so that the second object graph includes at least one dereferenced object.

BRIEF DESCRIPTION OF THE SEVERAL VIEWS OF THE DRAWINGS

Embodiments of the present invention will now be described, by way of example only, with reference to the following drawings in which:

FIG. 1 is a schematic illustration of a computer system comprising a virtual machine in accordance with an embodiment;

FIGS. 2 a and 2 b are schematic representations of object graphs in the memory of the virtual machine of FIG. 1 in accordance with an embodiment;

FIG. 3 is a table illustrating data extracted from one of the object graphs of FIG. 2 b in accordance with an embodiment;

FIG. 4 is a flow chart illustrating processing performed in the computer system in response to the dereferencing of an object in the object graph of FIG. 2 a in accordance with an embodiment; and

FIG. 5 is a flow chart illustrating processing performed in response to a garbage collection process performed on the object graph of FIG. 2 b resulting in the data of FIG. 3 in accordance with an embodiment.

DETAILED DESCRIPTION OF THE EMBODIMENTS

Embodiments of the present invention are directed to managing memory in a computer system in which memory locations in use at any given time are represented as a set of memory objects in a first object graph. The first object graph includes a system root object associated by references to each of the memory objects. Methods include creating a second root object for the memory so as to form a second object graph for the memory. In response to the dereferencing of a first object from the first object graph, the dereferenced first object is associated with the second object graph so that the second object graph includes one or more dereferenced objects.

Methods may also include identifying a second object in the first object graph that referenced the first object immediately prior to the dereferencing of the first object and creating a reference from the second object graph to the second object so as to associate the first object in the second object graph with the second object in the first object graph. Methods may also include creating a first metadata object in the second object graph the first metadata object being arranged to provide the reference from the second object graph to the second object in the first object graph and to reference the first object in the second object graph. Methods may further include identifying predetermined metadata for the first object identifying the location of the first object in the first object graph immediately prior to the dereferencing; and storing the metadata in the second object graph in association with the first object.

The metadata may be stored in a second metadata object associated with the first object in the second object graph. The metadata may include data identifying a field in the second object used to reference the first object immediately prior to the dereferencing of the first object from the second object.

Methods may also include, in response to a first stage of garbage collection in which a first set of objects including all objects present in the first object graph being identified, identifying a second set of objects that include objects present in the second object graph and not present in the first object graph. Predetermined data associated with each of the objects in the second set of objects is saved prior to any further stage of garbage collection in which the second set of objects is deleted from the memory. The predetermined data may include data identifying the first object. The predetermined data may include data identifying the second object that referenced the first object immediately prior to the dereferencing of the first object. The predetermined data may include data identifying the field in the second object that referenced the first object in the first object graph immediately prior to the dereferencing.

Other embodiments provide an apparatus for managing memory in a computer system in which memory locations in use at any given time are represented as a set of memory objects in a first object graph that includes a system root object associated by references to each of the memory objects. The apparatus may be operable to create a second root object for the memory so as to form a second object graph for the memory, and in response to the dereferencing of a first object from the first object graph, associate the dereferenced first object with the second object graph so that the second object graph includes one or more dereferenced objects.

Further embodiments provide a computer program product for managing memory in a computer system in which memory locations in use at any given time are represented as a set of memory objects in a first object graph that includes a system root object associated by references to each of the memory objects. The computer program product may include a computer-readable storage medium having computer-readable program code embodied therewith, the computer-readable program code configured to: create a second root object for the memory so as to form a second object graph for the memory; and in response to the dereferencing of a first object from the first object graph, associate the dereferenced first object with the second object graph so that the second object graph includes one or more dereferenced objects.

Embodiments of the invention are arranged to preserve data relating to memory objects removed from the system object graph for use in identifying the source of such removed objects.

With reference to FIG. 1, a computer 101 is loaded with an operating system 102, which provides a processing platform for one or more application programs. In the present embodiment, the computer is loaded with a virtual machine environment application program 103, in the form of a Java runtime environment (JRE) application program, arranged to enable a user to run one or more Java virtual machines (JVMs) 104 on the computer 101. Each JVM 104 includes virtual memory 105 and a memory manager program 106 that includes a garbage collection module 107. Each JVM 104 further includes storage 107 arranged to store one or more programs 108 for running on the JVM 104 and to store input or output program data 109.

The memory manager 106 is arranged to manage the use of the memory 105 during the processing by the JVM 104 of one of the programs 108 by allocating or de-allocating portions of the memory 105 to the program 108 as required. The memory manager 106 periodically performs a garbage collection process to scan the memory 105 to identify portions of the memory 105 that have been de-allocated by a program 108, clear data from any such portions of memory and mark the portions of memory as available for further allocation by the memory manager 106 to a program 108.

In the memory model for the JVM 104 the memory 105 is initially free and allocated to a memory management data construct call the heap from which locations in the memory 105 are allocated on request from a program 108 for storage of a memory object in the form of a variable, function or data structure. With reference to the embodiment shown in FIG. 2 a, the memory objects 201 in use at any given time by a program 108 may be associated by means of references 202 from one object 201 to another so as to form a first object graph 203 in the form of a system object graph. The system object graph 203 shown in FIG. 2 a includes a root object 204 referred to as the system root. In other words, the current or live data objects for a program 108 are all associated by references 202 to the system root 204 either directly or indirectly via one or more other objects 201. Any given object 201 may be referenced by any number of other objects 201. In other words, a given object 201 may be linked or associated with any number of other objects 201 by references from each of those other objects 201.

When a reference 202 to a given object 201 is no longer required by a program 108, the relevant reference 202 is removed from the object graph 203. In other words, the object 201 is de-referenced. However, since an object 201 may be referenced by any number of other objects 201, the removal of one reference does not necessarily indicate that the object has been discarded. Only once all references 202 to a given object have been removed can the object 201 be treated as discarded. Since all references 202 to a discarded object 201 will thus have been removed, the discarded object will no longer be linked or associated with the system root 204. The garbage collection process of the memory manager 106 is arranged to traverse the object graph for the memory 105 and identify all objects 201 that are linked either directly or indirectly to the system root 204. Such objects are referred to as live objects, that is, objects that are currently in use by the relevant program 108. All other objects 201 are treated as discarded or dead objects and can thus be removed from memory 105 and their allocated memory returned to the heap for reuse.

In an embodiment, the memory manager 106 is arranged to collect data for objects 201 in response to their dereferencing. During the garbage collection process, the memory manager 106 is further arranged, for any dereferenced object that is dead and to be discarded, to save a predetermined set of data relating to the dead object in the form of dead object data 110 shown in FIG. 1. In an embodiment, the dead object data includes an identification of the dead object, a reference to the live object from which the dead object was de-referenced, and field metadata identifying the field in the live object that contained the removed reference to the dead object.

With reference to the embodiment shown in FIG. 2 b, in order to collect the dead object data 110, the memory manager 106 is arranged to maintain a second object graph 205 for the memory 105 which includes the dereferenced objects. In an embodiment, the second object graph 205 includes a root object 206 referred to herein as the dead root. The memory manager 106 is arranged, in response to the dereferencing of an object from the first object graph 203, that is, the system root object graph, to add the dereferenced object to the dereferenced object graph 205. In an embodiment, this is implemented by adding a new object to the dereferenced object graph 205 in the form of a dead object information object 207. In an embodiment, all new dead object information objects are referenced directly from the dead root object 206. The dead object information object 207 includes a reference 208 to the newly dereferenced object 209. In an embodiment, the dead information object 207 further includes a reference 210 to the live object 201 from which the dereferenced object 209 was de-referenced. A field definition object 211 is also added to the dereferenced object graph 205 and referenced from the dead object information object 207. The field definition object 211 includes data identifying the field in the live object 201 that contained the removed reference to the newly dereferenced object 209.

The garbage collection process performed by the memory manager 106 is arranged, in a first phase commonly referred to as the marking phase, to search the system root object graph 203 and mark all connected objects present as “live”. In other words, all objects that are referenced either directly from the system root 204 or indirectly from the system root 204 via one or more other objects 201 in the system root object graph 203 are identified as currently in use by a loaded program 108. The dereferenced object graph 205 not used for identifying “live” objects. Nevertheless, some of the objects identified via the system root object graph 203 may also be present in the dereferenced object graph 205. This occurs where a dereferenced object is referenced by more than one live object and one or more such references remain when the garbage collection process is performed.

In a second phase of the garbage collection process, the memory manager 106 is arranged to search the dereferenced object graph 205 to identify any connected object 209 that was not identified as “live” in the first phase and is thus a candidate for removal or sweeping form the memory 105. The garbage collection is arranged to exclude from this search any metadata objects, that is, in the present embodiment, the dead information objects 207 and the field definition objects 211. This avoids the unnecessary processing by the garbage collection process of metadata objects, which, in the present embodiment, are never referenced by a “live” object. For each such candidate object the associated objects in the form of the dead object information object 207 and the metadata object 211 that were created on dereferencing of the object are identified and the predetermined dead object data 110 extracted and stored as shown in the embodiment shown in FIG. 3. Thus, for each relevant object, the dead object data 110 firstly includes an object identifier 301 identifying the dead object 209. Secondly the dead object data 110 includes an identification 302 of the original parent 201 of the dead object 209 that is the object 201 that originally referenced the dead object 209. The identification 302 is derived from the reference 210 from the dead object information object 207 to the originally referencing object 201. Thirdly the dead object data 110 includes referencing field data 303 identifying the field in the object 201 that was used for originally referencing the dead object 209. The referencing field data 303 is extracted from the field definition object 211 referenced from the dead object information object 207.

Some objects may be present in the dereferenced object graph 205 that remain referenced from one or more live objects in the system root object graph 203 and thus remain “live”. However, in the first phase of the garbage collection process the metadata objects associated with such dereferenced but still “live” objects will not get marked as “live” as they are inaccessible from the system root 204 and will thus be candidates for removal. Therefore, in order to preserve the metadata objects for such dereferenced but still “live” objects, the memory manager 106 is further arranged to identify any of the dereferenced objects in the second object graph 205 marked as “live” and to similarly mark their associated metadata objects as “live” to avoid their subsequent removal.

Once the dead object data 110 has been extracted from the relevant nodes of the dereferenced object graph 205 a third phase in the garbage collection process is initiated, commonly referred to as the sweeping phase. In this third phase, all objects in the memory 105 are scanned and all objects not marked as “live” are removed and their memory allocation returned to the heap for reuse. Where objects are removed from memory 105, the dereferenced object graph 205 is repaired to remove the now redundant metadata objects, that is, in an embodiment, the relevant dead information objects 207 and the field definition objects 211. Some objects may remain in the dereferenced object graph 205 as they are still referenced from one or more live objects in the system root object graph 203.

An embodiment of the processing performed by the memory manager 106 in response to the dereferencing of an object will now be described with reference of the flow chart of FIG. 4. Processing is initiated at block 401 in response to the selection of an object for dereferencing and processing then moves to block 402. At block 402, the selected object is dereferenced from the system root object graph 203 and processing moves to block 403. At block 403, if no dead root object currently exists in the memory 105 then processing moves to block 404. At block 404, a dead root object 206 is created and processing moves to block 405. If, at block 403, a dead root object 206 is identified in the memory 105 then processing moves straight to block 405. At block 405, a new dead object information object 207 is created and a reference is added from the dead root object 206 to the new dead object information object 207 and processing moves to block 406. At block 406, a reference is added from the new dead object information object 207 to the live object 201 that referenced the newly dereferenced object 209 immediately prior to its dereferencing and processing moves to block 407. At block 407, a reference is added from the new dead object information object 207 to the newly dereferenced object 209 and processing moves to block 408. At block 408, a new metadata object 211 is created and data identifying the field in the prior referencing object 201 that provided the reference to the newly dereferenced object 209 and processing moves to block 409. At block 409, a reference to the new metadata object 211 is added to the dead object information object 207. Processing then moves to block 410 and ends.

An embodiment of the processing performed by the memory manager 106 in the garbage collection process will now be described with reference of the flow chart of FIG. 5. Processing is initiated at block 501 in accordance with the garbage collection scheduling of the memory manager 106 and processing then moves to block 502. At block 502, the system root object graph 203 is traversed and all accessible objects marked as live and processing moves to block 503. At block 503, the dereferenced object graph 205 is traversed to identify any dereferenced objects marked as “live”, identify their associated metadata objects and mark those objects as “live” and processing moves to block 504. At block 504, the dereferenced object graph 205 is traversed to identify any connected object 209 that was not identified as “live” in block 502 and is thus a candidate object for removal from memory 105 and processing moves to block 505. At block 505, for each identified candidate for removal, the dead object data 110 is extracted from the relevant objects in the dereferenced object graph 205 and stored and processing moves to block 506. At block 506, all candidate objects for removal identified in block 502 are removed from memory and their memory allocations returned to the heap and processing moves to block 507. At block 507, the dereferenced object graph 205 is repaired where necessary to take into account the removed objects. Processing then moves to block 508 and ends.

In another embodiment, the dead object data includes an identification of closest live object that references a given dead object either directly or indirectly via one or more other dead objects.

In a further embodiment, the memory manager provides memory allocations from two heaps. The first heap provides storage for all objects and the second heap provides storage for the dead root object and the metadata objects such as the dead information objects and the field definition objects. In response to the dereferencing of an object in the first heap, a corresponding set of metadata objects is created in the second heap including a cross-heap reference to the dereferenced object in the first heap. When the garbage collection process is applied to the first heap any dereferenced object with no further references from live objects will be identified as a candidate for removal. The cross-heap reference from the metadata objects in the second heap will not be visible and thus not disrupt the identification of the dereferenced object by the garbage collection process. For each object identified as a candidate for removal, the second heap is scanned to identify and output the associated object metadata. The cross-heap reference from the metadata to the associated object in the first heap is then removed. A second garbage collection process is then performed on the second heap starting from the dead root and arranged to identify and remove any metadata objects not connected to a live object, that is, without a cross-heap reference to a live object.

As will be understood by those skilled in the art embodiments of the invention are not limited to Java or the JRE and may be applied to any virtual machine system or environment. Suitable virtual machine systems or environments may be arranged to run directly on a computer system or run on an operating system. In other words, the virtual machine system or environment may run natively or be hosted. The virtual machines may be provided by software emulation or hardware virtualization.

As will be understood by those skilled in the art, embodiments of the invention may be applied to the physical machine environment, that is, to operating systems running directly on a physical computer and providing a platform for running one or more application programs.

As will be understood by those skilled in the art any suitable level of dead object data may be provided for a given application. In some applications, minimal dead object data may be provided which provides a single data item for each dead object such as an identifier for the dead object or an identification of the object that referenced the dead object immediately prior to its dereferencing or an identification of closest live object that references a given dead object either directly or indirectly via one or more other dead objects.

Embodiments of the invention reference the metadata objects associated with dereferenced objects from the dead root and the dereferenced object itself is referenced from the metadata. This avoids the need for additional fields in objects for referencing their respective metadata. Such additional fields may need to be hidden in some implementations.

Embodiments of the invention are arranged to preserve data relating to the memory objects that have been created during the processing of programs on a computer that have subsequently been removed from memory as a result of a garbage collection process or other suitable memory management process. The data may include an identification of the removed objects or detail of the objects that were associated with the removed object or referenced the removed object. The data may include identification of the fields of referencing objects from which the removed object was referenced. The dead object data can be used for identifying the programs, parts of programs or other processes that created the respective objects. Such identification is useful for monitoring, measuring, modifying or improving the memory usage of the relevant program, program part or other process. The data may be used for reconstructing the system object graph at selected points in the associated processing.

It will be understood by those skilled in the art that the apparatus that embodies a part or all of the present invention may be a general purpose device having software arranged to provide a part or all of an embodiment of the invention. The device could be a single device or a group of devices and the software could be a single program or a set of programs. Furthermore, any or all of the software used to implement the invention can be communicated via any suitable transmission or storage means so that the software can be loaded onto one or more devices.

While the present invention has been illustrated by the description of the embodiments thereof, and while the embodiments have been described in considerable detail, it is not the intention of the applicant to restrict or in any way limit the scope of the appended claims to such detail. Additional advantages and modifications will readily appear to those skilled in the art. Therefore, the invention in its broader aspects is not limited to the specific details of the representative apparatus and method, and illustrative examples shown and described. Accordingly, departures may be made from such details without departure from the scope of applicant's general inventive concept. 

What is claimed is:
 1. A method of managing memory in a computer system in which memory locations in use at any given time are represented as a set of memory objects in a first object graph that includes a system root object associated by references to each of said memory objects, the method comprising: creating a second root object for said memory so as to form a second object graph for said memory; and in response to a dereferencing of a first object from said first object graph, associating said dereferenced first object with said second object graph so that said second object graph comprises one or more dereferenced objects.
 2. A method according to claim 1, further comprising: identifying a second object in said first object graph that referenced said first object immediately prior to the dereferencing of said first object; and creating a reference from said second object graph to said second object so as to associate said first object in said second object graph with said second object in said first object graph.
 3. A method according to claim 2, further comprising: creating a first metadata object in said second object graph, said first metadata object being arranged to provide said reference from said second object graph to said second object in said first object graph and to reference said first object in said second object graph.
 4. A method according to claim 1, further comprising: identifying predetermined metadata for said first object that identifies the location of said first object in said first object graph immediately prior to said dereferencing; and storing said metadata in said second object graph in association with said first object.
 5. A method according to claim 4, wherein said metadata is stored in a second metadata object associated with said first object in said second object graph.
 6. A method according to claim 4, wherein said metadata comprises data identifying a field in said second object used to reference said first object immediately prior to said dereferencing of said first object from said second object.
 7. A method according to claim 1, further comprising: in response to a first stage of garbage collection in which a first set of objects comprising all objects present in said first object graph is identified, identifying a second set of objects comprising objects present in said second object graph and not present in said first object graph; and saving predetermined data associated with each of said objects in said second set of objects prior to any further stage of garbage collection in which said second set of objects is deleted from said memory.
 8. A method according to claim 7, wherein said predetermined data comprises data identifying said first object.
 9. A method according to claim 7, wherein said predetermined data comprises data identifying said second object that referenced said first object immediately prior to the dereferencing of said first object.
 10. A method according to claim 7, wherein said predetermined data comprises data identifying the field in said second object that referenced said first object in said first object graph immediately prior to said dereferencing.
 11. A system for managing memory in a computer system, the system comprising: a memory, wherein memory locations in the memory that are in use at any given time are represented as a set of memory objects in a first object graph, the first object graph including a system root object associated by references to each of the memory objects; and a processor, the system configured to: create a second root object for said memory so as to form a second object graph for said memory; and in response to a dereferencing of a first object from said first object graph, associate said dereferenced first object with said second object graph so that said second object graph comprises one or more dereferenced objects.
 12. The system according to claim 11, wherein the system is further configured to: identify a second object in said first object graph that referenced said first object immediately prior to the dereferencing of said first object; and create a reference from said second object graph to said second object so as to associate said first object in said second object graph with said second object in said first object graph.
 13. The system according to claim 12, wherein the system is further configured to: create a first metadata object in said second object graph said first metadata object being arranged to provide said reference from said second object graph to said second object in said first object graph and to reference said first object in said second object graph.
 14. The system according to claim 11, wherein the system is further configured to: identify predetermined metadata for said first object identifying the location of said first object in said first object graph immediately prior to said dereferencing; and store said metadata in said second object graph in association with said first object.
 15. The system according to claim 14, wherein said metadata is stored in a second metadata object associated with said first object in said second object graph.
 16. The system according to claim 14, wherein said metadata comprises data identifying a field in said second object used to reference said first object immediately prior to said dereferencing of said first object from said second object.
 17. The system according to claim 11, wherein the system is further configured to: in response to a first stage of garbage collection in which a first set of objects comprising all objects present in said first object graph is identified, identify a second set of objects comprising objects present in said second object graph and not present in said first object graph; and save predetermined data associated with each of said objects in said second set of objects prior to any further stage of garbage collection in which said second set of objects is deleted from said memory.
 18. The system according to claim 17, wherein said predetermined data comprises data identifying said first object.
 19. The system according to claim 17, wherein said predetermined data comprises data identifying said second object that referenced said first object immediately prior to the dereferencing of said first object.
 20. A computer program product for managing memory in a computer system in which memory locations in use at any given time are represented as a set of memory objects in a first object graph that includes a system root object associated by references to each of said memory objects, the computer program product comprising: a computer-readable storage medium having computer-readable program code embodied therewith, the computer-readable program code configured to: create a second root object for said memory so as to form a second object graph for said memory; and in response to a dereferencing of a first object from said first object graph, associate said dereferenced first object with said second object graph so that said second object graph comprises one or more dereferenced objects. 